home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / misc1 / iv26_w30.zip / INTERVIE / GRAPHIC / BASE.H next >
C/C++ Source or Header  |  1991-12-10  |  14KB  |  424 lines

  1. /*
  2.  * Copyright (c) 1987, 1988, 1989 Stanford University
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and its
  5.  * documentation for any purpose is hereby granted without fee, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Stanford not be used in advertising or
  9.  * publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  Stanford makes no representations about
  11.  * the suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  16.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  19.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  20.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. /*
  24.  * Interface to Graphic base class and FullGraphic, a subclass of Graphic
  25.  * for which all graphics state is defined.
  26.  */
  27.  
  28. #ifndef base_h
  29. #define base_h
  30.  
  31. #include <InterViews/painter.h>
  32. #include <InterViews/Graphic/geomobjs.h>
  33. #include <InterViews/Graphic/ppaint.h>
  34.  
  35. static const int UNDEF = -1;
  36.  
  37. class Canvas;
  38. class GraphicToPainter;
  39.  
  40. class Graphic : public Persistent {
  41. public:
  42.     Graphic(Graphic* gr = nil);
  43.     virtual ~Graphic();
  44.  
  45.     virtual void Draw(Canvas*);
  46.     virtual void Draw(Canvas*, Coord, Coord, Coord, Coord);
  47.     virtual void DrawClipped(Canvas*, Coord, Coord, Coord, Coord);
  48.     virtual void Erase(Canvas*);
  49.     virtual void Erase(Canvas*, Coord, Coord, Coord, Coord);
  50.     virtual void EraseClipped(Canvas*, Coord, Coord, Coord, Coord);
  51.  
  52.     virtual void FillBg(boolean);
  53.     virtual int BgFilled();
  54.     virtual void SetColors(PColor* f, PColor* b);
  55.     virtual PColor* GetFgColor();
  56.     virtual PColor* GetBgColor();
  57.     virtual void SetPattern(PPattern*);
  58.     virtual PPattern* GetPattern();
  59.     virtual void SetBrush(PBrush*);
  60.     virtual PBrush* GetBrush();
  61.     virtual void SetFont(PFont*);
  62.     virtual PFont* GetFont();
  63.  
  64.     void Translate(float dx, float dy);
  65.     void Scale(float sx, float sy, float ctrx = 0.0, float ctry = 0.0);
  66.     void Rotate(float angle, float ctrx = 0.0, float ctry = 0.0);
  67.     void Align(Alignment, Graphic*, Alignment);
  68.     void SetTransformer(Transformer*);
  69.     Transformer* GetTransformer();
  70.     void TotalTransformation(Transformer&);
  71.  
  72.     void GetBounds(float&, float&, float&, float&);
  73.     void GetBox(Coord&, Coord&, Coord&, Coord&);
  74.     void GetBox(BoxObj&);
  75.     virtual void GetCenter(float&, float&);    
  76.     virtual boolean Contains(PointObj&);
  77.     virtual boolean Intersects(BoxObj&);
  78.  
  79.     void SetTag(Ref);
  80.     Ref GetTag();
  81.  
  82.     Graphic* Parent();
  83.     virtual boolean HasChildren ();
  84.     virtual Graphic& operator = (Graphic&);
  85.     virtual Graphic* Copy();
  86.     virtual Persistent* GetCluster();
  87.     virtual ClassId GetClassId();
  88.     virtual boolean IsA(ClassId);
  89. protected:
  90. /*
  91.  * Member functions that declare a "Graphic* gs" parameter
  92.  * take into account that graphic's graphic state information when
  93.  * performing their function.  This is useful in hierarchical graphic objects
  94.  * (such as Pictures) where higher-level graphics' graphics state influences
  95.  * lower (and ultimately leaf) graphics'.
  96.  */
  97.     void update(Graphic* gs);            /* updates painter w/gs' state */
  98.     virtual void draw(Canvas*, Graphic* gs);
  99.     virtual void drawClipped(Canvas*, Coord, Coord, Coord, Coord, Graphic* gs);
  100.     virtual void erase(Canvas*, Graphic* gs);
  101.     virtual void eraseClipped(Canvas*, Coord, Coord, Coord, Coord,Graphic* gs);
  102. /*
  103.  * Bounding box operations.
  104.  */
  105.     virtual void getExtent(float&, float&, float&, float&, float&,Graphic* gs);
  106.         /* Returns lower-left and center coordinates, and a tolerance (tol)
  107.      * (in canvas coordinates) by which the final extent will be grown
  108.      * in each direction (i.e. l-=tol, b-=tol, r+=tol, t+=tol).
  109.      */
  110.     void GetExtent(Extent& e);
  111.     void getBounds(float&, float&, float&, float&, Graphic* gs);
  112.     void getBox(Coord&, Coord&, Coord&, Coord&, Graphic* gs);
  113.     void getBox(BoxObj&, Graphic* gs);
  114.     virtual boolean contains(PointObj&, Graphic* gs);
  115.     virtual boolean intersects(BoxObj&, Graphic* gs);
  116. /*
  117.  * Parent-related operations.
  118.  */
  119.     Graphic* getRoot();                /* top level parent */
  120.     void totalGS(Graphic& p);
  121.     void parentXform(Transformer& t);        /* parents' transformation */
  122.     void setParent(Graphic*, Graphic* parent);
  123.     void unsetParent(Graphic*);
  124. /*
  125.  * Bounding box caching operations.
  126.  */
  127.     void cachingOn();
  128.     void cachingOff();
  129.     virtual boolean extentCached();
  130.     virtual void uncacheExtent();
  131.     virtual void uncacheParents();
  132.     virtual void uncacheChildren();
  133.     virtual void invalidateCaches();
  134. /*
  135.  * Graphics state concatentation operations.
  136.  */
  137.     virtual void concatGS(Graphic* a, Graphic* b, Graphic* dest);
  138.     virtual void concatTransformer(
  139.         Transformer* a, Transformer* b, Transformer* dest
  140.     );
  141.     virtual void concat(Graphic* a, Graphic* b, Graphic* dest);
  142. /*
  143.  * Convenient transformations that check first if there's a transformer and
  144.  * then perform the (inverse) transformation.  The functions use the
  145.  * transformer of the supplied Graphic if there is one; otherwise this'
  146.  * transformer is used.
  147.  */
  148.     void transform(Coord& x, Coord& y, Graphic* = nil);
  149.     void transform(Coord x, Coord y, Coord& tx, Coord& ty, Graphic* = nil);
  150.     void transform(float x, float y, float& tx, float& ty, Graphic* = nil);
  151.     void transformList(
  152.         Coord x[], Coord y[], int n, Coord tx[], Coord ty[], Graphic* = nil
  153.     );
  154.     void transformRect(
  155.     float, float, float, float, 
  156.         float&, float&, float&, float&, Graphic* = nil
  157.     );
  158.     void invTransform(Coord& tx, Coord& ty, Graphic* = nil);
  159.     void invTransform(Coord tx, Coord ty, Coord& x, Coord& y, Graphic* = nil);
  160.     void invTransform(float tx, float ty, float& x, float& y, Graphic* = nil);
  161.     void invTransformList(
  162.         Coord tx[], Coord ty[], int n, Coord x[], Coord y[], Graphic* = nil
  163.     );
  164.     void invTransformRect(
  165.     float, float, float, float, 
  166.         float&, float&, float&, float&, Graphic* = nil
  167.     );
  168. /*
  169.  * Painter-equivalent rendering operations.  Graphic subclasses should use
  170.  * to draw themselves instead of using a painter directly.
  171.  */
  172.     void pText(Canvas* c, char* s, int n, Coord x, Coord y);
  173.     void pPoint(Canvas* c, Coord x, Coord y);
  174.     void pMultiPoint(Canvas*, Coord x[], Coord y[], int);
  175.     void pLine(Canvas*, Coord, Coord, Coord, Coord);
  176.     void pRect(Canvas*, Coord, Coord, Coord, Coord);
  177.     void pFillRect(Canvas*, Coord, Coord, Coord, Coord);
  178.     void pRasterRect(Canvas*, Coord, Coord, Raster*);
  179.     void pStencil(Canvas*, Coord, Coord, Bitmap*, Bitmap*);
  180.     void pCircle(Canvas*, Coord, Coord, int);
  181.     void pFillCircle(Canvas*, Coord, Coord, int);
  182.     void pEllipse(Canvas*, Coord, Coord, int, int);
  183.     void pFillEllipse(Canvas*, Coord, Coord, int, int);
  184.     void pMultiLine(Canvas*, Coord x[], Coord y[], int);
  185.     void pPolygon(Canvas*, Coord x[], Coord y[], int);
  186.     void pFillPolygon(Canvas*, Coord x[], Coord y[], int);
  187.     void pBSpline(Canvas*, Coord x[], Coord y[], int);
  188.     void pClosedBSpline(Canvas*, Coord x[], Coord y[], int);
  189.     void pFillBSpline(Canvas*, Coord x[], Coord y[], int);
  190. /*
  191.  * "Helper" functions that allow graphic subclasses to call
  192.  * the protected member functions redefined by other graphic subclasses.
  193.  */
  194.     void drawGraphic(Graphic*, Canvas*, Graphic*);
  195.     void drawClippedGraphic(Graphic*,Canvas*,Coord,Coord,Coord,Coord,Graphic*);
  196.     void eraseGraphic(Graphic*, Canvas*, Graphic*);
  197.     void eraseClippedGraphic(
  198.         Graphic*, Canvas*, Coord, Coord, Coord, Coord, Graphic*
  199.     );
  200.  
  201.     void getExtentGraphic(
  202.         Graphic*, float&, float&, float&, float&, float&, Graphic* gs
  203.     );
  204.     boolean containsGraphic(Graphic*, PointObj&, Graphic* gs);
  205.     boolean intersectsGraphic(Graphic*, BoxObj&, Graphic* gs);
  206.  
  207.     boolean extentCachedGraphic(Graphic*);
  208.     void uncacheExtentGraphic(Graphic*);
  209.     void uncacheParentsGraphic(Graphic*);
  210.     void uncacheChildrenGraphic(Graphic*);
  211.     void invalidateCachesGraphic(Graphic*);
  212.  
  213.     void concatGSGraphic(Graphic*, Graphic*, Graphic*, Graphic*);
  214.     void concatTransformerGraphic(
  215.         Graphic*,Transformer*, Transformer*, Transformer*
  216.     );
  217.     void concatGraphic(Graphic*, Graphic*, Graphic*, Graphic*);
  218. /*
  219.  * Persistent read/write operations.
  220.  */
  221.     virtual boolean read(PFile*);
  222.     virtual boolean write(PFile*);
  223. protected:
  224.     static Transformer* identity;   /* identity matrix */
  225.     static boolean caching;        /* state of bounding box caching */
  226. private:
  227.     Ref parent;
  228.     int fillBg;
  229.     Ref fg;
  230.     Ref bg;
  231.     Ref tag;
  232.     Transformer* t;
  233.     static GraphicToPainter* painters;
  234.     static Painter* p;
  235. };
  236.  
  237. class FullGraphic : public Graphic {
  238. public:
  239.     FullGraphic(Graphic* = nil);
  240.  
  241.     virtual void SetPattern(PPattern*);
  242.     virtual PPattern* GetPattern();
  243.     virtual void SetBrush(PBrush*);
  244.     virtual PBrush* GetBrush();
  245.     virtual void SetFont(PFont*);
  246.     virtual PFont* GetFont();
  247.  
  248.     virtual Graphic* Copy();
  249.     virtual ClassId GetClassId();
  250.     virtual boolean IsA(ClassId);
  251. protected:
  252.     virtual boolean read(PFile*);
  253.     virtual boolean write(PFile*);
  254. private:
  255.     Ref pat;
  256.     Ref brush;
  257.     Ref font;
  258. };
  259.  
  260. /*
  261.  * inlines
  262.  */
  263.  
  264. inline Transformer* Graphic::GetTransformer () { return t; }
  265. inline Graphic* Graphic::Parent () { return (Graphic*) parent(); }
  266. inline void Graphic::SetTag (Ref r) { tag = r; }
  267. inline Ref Graphic::GetTag () { return tag; }
  268.  
  269. inline void Graphic::GetBox (BoxObj& b) { 
  270.     GetBox(b.left, b.bottom, b.right, b.top);
  271. }
  272.  
  273. inline void Graphic::getBox (BoxObj& b, Graphic* p) {
  274.     getBox(b.left, b.bottom, b.right, b.top, p);
  275. }
  276.  
  277. inline void Graphic::pText (Canvas* c, char* s, int n, Coord x, Coord y) {
  278.     p->Text(c, s, n, x, y);
  279. }
  280.  
  281. inline void Graphic::pPoint (Canvas* c, Coord x, Coord y) {
  282.     p->Point(c, x, y);
  283. }
  284.  
  285. inline void Graphic::pMultiPoint(Canvas* c, Coord x[], Coord y[], int n) {
  286.     p->MultiPoint(c, x, y, n);
  287. }
  288.  
  289. inline void Graphic::pLine(Canvas* c, Coord x1, Coord y1, Coord x2, Coord y2) {
  290.     p->Line(c, x1, y1, x2, y2);
  291. }
  292.  
  293. inline void Graphic::pRect(Canvas* c, Coord x1, Coord y1, Coord x2, Coord y2) {
  294.     p->Rect(c, x1, y1, x2, y2);
  295. }
  296.  
  297. inline void Graphic::pFillRect(Canvas* c,Coord x1,Coord y1, Coord x2,Coord y2){
  298.     p->FillRect(c, x1, y1, x2, y2);
  299. }
  300.  
  301. inline void Graphic::pRasterRect(Canvas* c, Coord x, Coord y, Raster* r){
  302.     p->RasterRect(c, x, y, r);
  303. }
  304.  
  305. inline void Graphic::pStencil(Canvas* c,Coord x,Coord y, Bitmap* i, Bitmap* m){
  306.     p->Stencil(c, x, y, i, m);
  307. }
  308.  
  309. inline void Graphic::pCircle(Canvas* c, Coord x, Coord y, int r) {
  310.     p->Circle(c, x, y, r);
  311. }
  312.  
  313. inline void Graphic::pFillCircle(Canvas* c, Coord x, Coord y, int r) {
  314.     p->FillCircle(c, x, y, r);
  315. }
  316.  
  317. inline void Graphic::pEllipse(Canvas* c, Coord x, Coord y, int r1, int r2) {
  318.     p->Ellipse(c, x, y, r1, r2);
  319. }
  320.  
  321. inline void Graphic::pFillEllipse(Canvas* c, Coord x, Coord y, int r1, int r2){
  322.     p->FillEllipse(c, x, y, r1, r2);
  323. }
  324.  
  325. inline void Graphic::pMultiLine(Canvas* c, Coord x[], Coord y[], int n) {
  326.     p->MultiLine(c, x, y, n);
  327. }
  328.  
  329. inline void Graphic::pPolygon(Canvas* c, Coord x[], Coord y[], int n) {
  330.     p->Polygon(c, x, y, n);
  331. }
  332.  
  333. inline void Graphic::pFillPolygon(Canvas* c, Coord x[], Coord y[], int n) {
  334.     p->FillPolygon(c, x, y, n);
  335. }
  336.  
  337. inline void Graphic::pBSpline(Canvas* c, Coord x[], Coord y[], int n) {
  338.     p->BSpline(c, x, y, n);
  339. }
  340.  
  341. inline void Graphic::pClosedBSpline(Canvas* c, Coord x[], Coord y[], int n) {
  342.     p->ClosedBSpline(c, x, y, n);
  343. }
  344.  
  345. inline void Graphic::pFillBSpline(Canvas* c, Coord x[], Coord y[], int n) {
  346.     p->FillBSpline(c, x, y, n);
  347. }
  348.  
  349. inline void Graphic::getBounds (
  350.     float& l, float& b, float& r, float& t, Graphic* gs
  351. ) {
  352.     float tol;
  353.  
  354.     getExtent(l, b, r, t, tol, gs);
  355.     r += r - l;
  356.     t += t - b;
  357.     l -= tol;
  358.     b -= tol;
  359.     r += tol;
  360.     t += tol;
  361. }
  362.  
  363. inline void Graphic::drawGraphic (Graphic* g, Canvas* c, Graphic* gs) {
  364.      g->draw(c, gs);
  365. }
  366. inline void Graphic::eraseGraphic (Graphic* g, Canvas* c, Graphic* gs) {
  367.     g->erase(c, gs);
  368. }
  369.  
  370. inline void Graphic::drawClippedGraphic (
  371.     Graphic* g, Canvas* c, Coord l, Coord b, Coord r, Coord t, Graphic* gs
  372. ) { g->drawClipped(c, l, b, r, t, gs); }
  373.  
  374. inline void Graphic::eraseClippedGraphic (
  375.     Graphic* g, Canvas* c, Coord l, Coord b, Coord r, Coord t, Graphic* gs
  376. ) { g->eraseClipped(c, l, b, r, t, gs); }
  377.  
  378. inline void Graphic::getExtentGraphic (
  379.     Graphic* g, float& l, float& b, float& r, float& t, float& tol, Graphic* gs
  380. ) { g->getExtent(l, b, r, t, tol, gs); }
  381.  
  382. inline boolean Graphic::containsGraphic (Graphic* g, PointObj& p, Graphic* gs){
  383.     return g->contains(p, gs);
  384. }
  385.  
  386. inline boolean Graphic::intersectsGraphic (Graphic* g, BoxObj& b, Graphic* gs){
  387.     return g->intersects(b, gs);
  388. }
  389.  
  390. inline boolean Graphic::extentCachedGraphic (Graphic* g) {
  391.     return g->extentCached();
  392. }
  393.  
  394. inline void Graphic::uncacheExtentGraphic (Graphic* g) { g->uncacheExtent(); }
  395. inline void Graphic::uncacheParentsGraphic (Graphic* g) { g->uncacheParents();}
  396.  
  397. inline void Graphic::uncacheChildrenGraphic (Graphic* g) {
  398.     g->uncacheChildren();
  399. }
  400.  
  401. inline void Graphic::invalidateCachesGraphic (Graphic* g) {
  402.     g->invalidateCaches();
  403. }
  404.  
  405. inline void Graphic::concatGSGraphic (
  406.     Graphic* g, Graphic* a, Graphic* b, Graphic* d
  407. ) {
  408.     g->concatGS(a, b, d);
  409. }
  410.  
  411. inline void Graphic::concatTransformerGraphic (
  412.     Graphic* g, Transformer* a, Transformer* b, Transformer* dest
  413. ) {
  414.     g->concatTransformer(a, b, dest);
  415. }
  416.  
  417. inline void Graphic::concatGraphic (
  418.     Graphic* g, Graphic* a, Graphic* b, Graphic* d
  419. ) {
  420.     g->concat(a, b, d);
  421. }
  422.  
  423. #endif
  424.